package com.water.devtool.ui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.common.io.Files;
import com.water.devtool.Const;
import com.water.devtool.define.DbConfigBean;
import com.water.devtool.define.DefineBean;
import com.water.devtool.define.EntityBean;
import com.water.devtool.define.ModularBean;
import com.water.devtool.define.ProjectBean;
import com.water.devtool.ui.define.ClassEditComposite;
import com.water.devtool.ui.define.ColumnEditComposite;
import com.water.devtool.ui.define.EntityComposite;
import com.water.devtool.ui.define.LevelConfigComposite;
import com.water.devtool.ui.define.ModularComposite;
import com.water.devtool.ui.define.ProjectComposite;
import com.water.freemarker.FreeMarkerUtil;
import com.water.util.ConfUtil;
import com.water.util.JsonUtil;
import com.water.util.PathUtil;
import com.watermud.util.FileUtil;
import com.watermud.util.Function;
import com.watermud.util.IteratorContext;
import com.watermud.util.IteratorUtil;

import swing2swt.layout.BorderLayout;

public class CodeGenerateUI {
	private final static Logger log = LoggerFactory.getLogger(CodeGenerateUI.class);

	protected Shell shell;
	private Tree tree;
	private TabFolder tabFolder;

	private String projectFile;
	private ProjectBean project;
	private boolean saveFile = true;
	private TreeItem trtmNewTreeitem;

	private Map<DefineBean, TreeItem> bindings;

	private final static String LAST_OPEN_FILE = "lastopen.txt";

	private Map<String, TabItem> tabMap = new HashMap<String, TabItem>();

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		CodeGenerateUI window = new CodeGenerateUI();
		window.open();
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		Rectangle area = Display.getDefault().getClientArea();
		shell.setSize(area.width, area.height);
		shell.setLocation(0, 0);
		shell.setText("代码生成工具");
		shell.setLayout(new BorderLayout(0, 0));

		ScrolledComposite scrolledComposite_1 = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		scrolledComposite_1.setMinWidth(200);
		scrolledComposite_1.setLayoutData(BorderLayout.WEST);
		scrolledComposite_1.setExpandHorizontal(true);
		scrolledComposite_1.setExpandVertical(true);

		tree = new Tree(scrolledComposite_1, SWT.BORDER);
		scrolledComposite_1.setContent(tree);

		Menu menu = new Menu(shell, SWT.BAR);
		shell.setMenuBar(menu);

		MenuItem mntmNewItem = new MenuItem(menu, SWT.CASCADE);
		mntmNewItem.setText("文件");
		Menu filemenu = new Menu(shell, SWT.DROP_DOWN);
		mntmNewItem.setMenu(filemenu);

		MenuItem menuItem_0 = new MenuItem(filemenu, SWT.NONE);
		menuItem_0.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				log.debug("点击载入菜单");
				loadFromFile();
			}
		});
		menuItem_0.setText("载入...");

		MenuItem menuItem = new MenuItem(filemenu, SWT.NONE);
		menuItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				log.debug("点击新建菜单");
				createProject();
			}
		});
		menuItem.setText("新建");

		MenuItem menuItem_1 = new MenuItem(filemenu, SWT.NONE);
		menuItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				log.debug("点击保存菜单");
				if (project == null) {
					saveFile = true;
					return;
				}
				if (StringUtils.isBlank(projectFile)) {
					DirectoryDialog filedlg = new DirectoryDialog(shell, SWT.OPEN);
					filedlg.setText("保存到目录:...");
					if (StringUtils.isNotBlank(LAST_OPEN_FILE)) {
						filedlg.setFilterPath(LAST_OPEN_FILE);
					} else {
						filedlg.setFilterPath("SystemRoot");
					}
					projectFile = filedlg.open();
				}

				if (projectFile == null) {
					return;
				}
				saveProject();
				saveFile = true;
			}
		});
		menuItem_1.setText("保存");

		MenuItem menuItem_2 = new MenuItem(filemenu, SWT.NONE);
		menuItem_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				log.debug("点击另存为菜单");

				if (project == null) {
					saveFile = true;
					return;
				}
				DirectoryDialog filedlg = new DirectoryDialog(shell, SWT.OPEN);
				filedlg.setText("保存到目录:...");
				if (StringUtils.isNotBlank(LAST_OPEN_FILE)) {
					filedlg.setFilterPath(LAST_OPEN_FILE);
				} else {
					filedlg.setFilterPath("SystemRoot");
				}
				projectFile = filedlg.open();

				if (projectFile == null) {
					return;
				}
				log.debug("保存到目录：{}", projectFile);
				saveProject();

				saveFile = true;
				File lastopenfile = new File(LAST_OPEN_FILE);
				try {
					Files.write(projectFile.getBytes(), lastopenfile);
				} catch (IOException e3) {
					// TODO Auto-generated catch block
					e3.printStackTrace();
				}

				MessageDialog.openInformation(shell, "提示", "保存成功！");

			}

		});
		menuItem_2.setText("另存为...");

		MenuItem menuItem_3 = new MenuItem(filemenu, SWT.NONE);
		menuItem_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!saveFile) {
					log.debug("提示请先保存");
					MessageDialog.openWarning(shell, "提示", "请先保存!");
					return;
				}
				System.exit(0);
			}
		});
		menuItem_3.setText("退出");

		tabFolder = new TabFolder(shell, SWT.NONE);
		tabFolder.setLayoutData(BorderLayout.CENTER);

		Menu menu2 = new Menu(tabFolder);
		MenuItem addProjectItem = new MenuItem(menu2, SWT.PUSH);
		addProjectItem.setText("关闭全部");
		addProjectItem.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				log.debug("点击 关闭全部");
				Set<String> keys = tabMap.keySet();
				for (String key : keys) {
					TabItem item = tabMap.get(key);
					item.dispose();
				}
				tabMap.clear();
			}

		});
		tabFolder.setMenu(menu2);

		initData();

		renderProject();

		initPopup();
	}

	private void saveProject() {
		Map<String, String> map = project.toMulString();
		File file = new File(projectFile);
		file.mkdirs();

		IteratorUtil.iterate(map, new Function<String>() {

			@Override
			public void execute(IteratorContext context, String json) {
				String key = (String) context.get("key");
				File temp = new File(file, key + ".json");
				try {
					log.debug("保存到文件：{}", temp.getCanonicalPath());
					Files.write(json.getBytes("utf-8"), temp);
				} catch (IOException e1) {
					log.error("保存文件出错", e1);
					MessageDialog.openInformation(shell, "提示", "保存文件出错!");
				}
			}

		});
	}

	private void initData() {
		File lastopenfile = new File(LAST_OPEN_FILE);
		if (lastopenfile.exists()) {
			projectFile = FileUtil.getContent(lastopenfile).trim();

			log.info("默认打开最后打开的项目：{}", projectFile);
			doLoad();
		}

	}

	protected void loadFromFile() {
		DirectoryDialog filedlg = new DirectoryDialog(shell);
		filedlg.setText("加载Project文件");
		if (StringUtils.isNotBlank(LAST_OPEN_FILE)) {
			filedlg.setFilterPath(LAST_OPEN_FILE);
		} else {
			filedlg.setFilterPath("SystemRoot");
		}
		projectFile = filedlg.open();
		if (projectFile != null) {
			doLoad();
		}
	}

	private void doLoad() {
		File dir = new File(projectFile);
		if (!dir.exists()) {
			try {
				MessageDialog.openInformation(shell, "提示", "加载失败，目录不存在：" + dir.getCanonicalPath());
			} catch (IOException e) {
				e.printStackTrace();
			}
			return;
		}

		String content = getFileContent(dir, "project.json");
		if (StringUtils.isBlank(content)) {
			MessageDialog.openInformation(shell, "提示", "加载失败，目录下面没有工程文件!");
			return;
		}
		log.debug("文件内容是：{}", content);
		try {
			project = JsonUtil.parse(content, ProjectBean.class);
		} catch (IOException e) {
			log.error("解析文件出错", e);
			MessageDialog.openInformation(shell, "提示", "解析文件出错!");
			return;
		}
		
		if(project.getVersionCode() > Const.versionCode) {
			MessageDialog.openInformation(shell, "提示", "请升级code-generete程序");
			return;
		}

		if (project.getVersionCode() < Const.versionCode) {
			project.setVersionCode(Const.versionCode);
			MessageDialog.openInformation(shell, "提示", "使用了新版本的code-generete，将工程配置文件改为最新版本");
		}
		

		// 分离工程目录，projectPath.json不用加入版本管理，不同开发人员工程保存在不同的位置
		String path = ConfUtil.getString("projectPath", null);
		if (StringUtils.isBlank(path)) {
			path = PathUtil.transcate(ConfUtil.getString("workspace", ""), project.getCode());
		}
		if (!StringUtils.isBlank(path)) {
			log.debug("工程路径是：{}", path);
			project.setProjectPath(path);
		}

		String dbContent = getFileContent(dir, "dbConfig.json");
		if (StringUtils.isNotBlank(dbContent)) {
			log.debug("数据库配置内容是：{}", dbContent);
			try {
				DbConfigBean dbConfig = JsonUtil.parse(dbContent, DbConfigBean.class);
				project.setDbConfig(dbConfig);
			} catch (JsonParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JsonMappingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		File[] fs = dir.listFiles();
		for (File file : fs) {
			String fileName = file.getName();
			if (fileName.startsWith("modular.")) {
				String tempContent = getFileContent(dir, fileName);
				log.debug("模块{}配置文件内容是：{}", fileName, tempContent);
				ModularBean m;
				try {
					m = JsonUtil.parse(tempContent, ModularBean.class);
					project.addModular(m);
				} catch (IOException e) {
					e.printStackTrace();
				}

			}
		}

		project.init();

		renderProject();
		File lastopenfile = new File(LAST_OPEN_FILE);
		try {
			Files.write(projectFile.getBytes(), lastopenfile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		shell.setText("代码生成工具：" + projectFile);

		FreeMarkerUtil.load(projectFile);
	}

	private String getFileContent(File dir, String name) {
		File pFile = new File(dir, name);
		if (!pFile.exists()) {
			return null;
		}
		byte[] bt = new byte[1024 * 1024 * 10];
		FileInputStream is;
		try {
			is = new FileInputStream(pFile);
		} catch (FileNotFoundException e1) {
			MessageDialog.openInformation(shell, "提示", "加载失败，文件不存在!");
			return null;
		}
		int length;
		try {
			length = is.read(bt);
		} catch (IOException e1) {
			MessageDialog.openInformation(shell, "提示", "文件读取错误！");
			return null;
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		String content;
		try {
			content = new String(bt, 0, length, "utf-8");
		} catch (UnsupportedEncodingException e2) {
			log.error("不支持UTF-8编码方式", e2);
			MessageDialog.openInformation(shell, "提示", "不支持UTF-8编码方式!");
			return null;
		}
		return content;
	}

	public void initPopup() {
		Menu menu = new Menu(tree);
		MenuItem addProjectItem = new MenuItem(menu, SWT.PUSH);
		addProjectItem.setText("新建项目");
		addProjectItem.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				log.debug("点击 新建项目");
				createProject();
			}

		});

		MenuItem addModularItem = new MenuItem(menu, SWT.PUSH);
		addModularItem.setText("新建模块");
		addModularItem.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				log.debug("点击 新建模块");
				createModular();
			}

		});
		MenuItem addEntityItem = new MenuItem(menu, SWT.PUSH);
		addEntityItem.setText("新建实体");
		addEntityItem.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				log.debug("点击 新建实体");
				createEntity();
			}

		});

		MenuItem dbItem = new MenuItem(menu, SWT.PUSH);
		dbItem.setText("配置数据库");
		dbItem.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				log.debug("点击 配置数据库");
				configDb();
			}

		});

		MenuItem levelItem = new MenuItem(menu, SWT.PUSH);
		levelItem.setText("配置层");
		levelItem.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				log.debug("配置层");
				configLevel();
			}
		});

		MenuItem deleteItem = new MenuItem(menu, SWT.PUSH);
		deleteItem.setText("删除");
		deleteItem.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				log.debug("点击 删除");
			}

		});

		tree.setMenu(menu);

		tree.addListener(SWT.MouseDoubleClick, new Listener() {
			public void handleEvent(Event event) {
				Point point = new Point(event.x, event.y);
				TreeItem item = tree.getItem(point);
				if (item == null) {
					log.debug("找不到节点");
					return;
				}
				Object type = item.getData("type");
				Object obj = item.getData("bean");
				if (type == null) {
					log.error("树节点不对");
					return;
				}
				TabItem tabItem = null;
				switch (type.toString()) {
				case "project":
					ProjectBean pro = (ProjectBean) obj;
					String pk = pro.getType() + pro.getCode();
					tabItem = openTab(pk, pro.getCode());
					ProjectComposite pc = new ProjectComposite(tabFolder, SWT.NONE);
					pc.setProject(pro);
					tabItem.setControl(pc);
					log.debug("双击项目：{}", pro.getCode());
					break;
				case "modular":
					ModularBean mod = (ModularBean) obj;
					String mk = mod.getType() + mod.getCode();
					tabItem = openTab(mk, mod.getCode());
					ModularComposite mc = new ModularComposite(tabFolder, SWT.NONE);
					mc.setModular(mod);
					tabItem.setControl(mc);
					log.debug("双击模块：{}", mod.getCode());
					break;

				case "entity":
					EntityBean entity = (EntityBean) obj;
					String ek = entity.getType() + entity.getCode();
					tabItem = openTab(ek, entity.getCode());
					EntityComposite ec = new EntityComposite(tabFolder, SWT.NONE);
					ec.setEntity(entity);
					tabItem.setControl(ec);
					log.debug("双击实体：{}", entity.getCode());
					break;
				case "column":
					EntityBean eb = (EntityBean) obj;
					String pkc = "COLUMN" + eb.getCode();
					tabItem = openTab(pkc, eb.getCode() + "-字段");
					ColumnEditComposite cc = new ColumnEditComposite(tabFolder, SWT.NONE);
					cc.setEntity(eb);
					tabItem.setControl(cc);
					break;
				case "class":
					EntityBean d = (EntityBean) obj;
					String pkd = "CLASS" + d.getCode();
					tabItem = openTab(pkd, d.getCode() + "-类");
					ClassEditComposite ccd = new ClassEditComposite(tabFolder, SWT.NONE);
					ccd.setEntity(d);
					tabItem.setControl(ccd);
					break;
				default:
					break;
				}

			}

		});
	}

	public void configLevel() {
		if (project == null) {
			log.debug("请先创建项目");
			MessageDialog.openInformation(shell, "提示", "请先创建项目!");
			return;
		}
		String pk = "LEVEL" + project.getCode();
		TabItem tabItem = openTab(pk, "层配置");
		LevelConfigComposite lc = new LevelConfigComposite(tabFolder, SWT.NONE);
		lc.setProject(project);
		tabItem.setControl(lc);

	}

	protected void createEntity() {
		TreeItem[] items = tree.getSelection();
		if (items == null || items.length == 0) {
			log.debug("请先选择模块");
			MessageDialog.openWarning(shell, "提示", "请先选择模块!");
			return;
		}
		TreeItem modularItem = items[0];
		Object obj = modularItem.getData("bean");
		if (obj instanceof ModularBean) {
			ModularBean modular = (ModularBean) obj;
			NameInputDialog dialog = new NameInputDialog(shell, "创建实体", "创建一个新的实体，请输入实体名称和描述内容");
			dialog.create();
			if (dialog.open() == Window.OK) {
				log.debug("创建实体：{}，描述内容：{}", dialog.getCode(), dialog.getDesc());

				EntityBean entity = new EntityBean();
				entity.setModular(modular);
				entity.setCode(dialog.getCode());
				entity.setDesc(dialog.getDesc());
				entity.setDefault();
				TreeItem parent = bindings.get(modular);
				renderEntity(parent, entity);
				modular.addEntity(entity);
			}
		} else {
			log.debug("请先选择模块");
			MessageDialog.openWarning(shell, "提示", "请先选择模块!");
			return;
		}

	}

	private TabItem openTab(String key, String title) {

		TabItem tabItem = tabMap.get(key);
		if (tabItem == null || tabItem.isDisposed()) {
			tabItem = new TabItem(tabFolder, SWT.SHELL_TRIM);
			tabItem.setText(title);
			tabMap.put(key, tabItem);
		}
		int count = tabFolder.getItemCount();
		TabItem[] items = tabFolder.getItems();
		for (int i = 0; i < count; i++) {
			TabItem item = items[i];
			if (item == tabItem) {
				tabFolder.setSelection(i);
				return item;
			}
		}
		return tabItem;
	}

	protected void createProject() {
		if (!saveFile) {
			log.debug("提示请先保存");
			MessageDialog.openWarning(shell, "提示", "请先保存!");
			return;
		}
		NameInputDialog dialog = new NameInputDialog(shell, "创建工程", "创建一个新的工程，请输入工程名称和描述内容");
		dialog.create();
		if (dialog.open() == Window.OK) {
			log.debug("创建工程：{}，描述内容：{}", dialog.getCode(), dialog.getDesc());
			projectFile = null;
			project = new ProjectBean();
			project.setCode(dialog.getCode());
			project.setDesc(dialog.getDesc());
			renderProject();

		}

	}

	protected void createModular() {
		if (project == null) {
			log.debug("请先创建项目");
			MessageDialog.openInformation(shell, "提示", "请先创建项目!");
			return;
		}
		NameInputDialog dialog = new NameInputDialog(shell, "创建模块", "创建一个新的模块，请输入模块名称和描述内容");
		dialog.create();
		if (dialog.open() == Window.OK) {
			log.debug("创建模块：{}，描述内容：{}", dialog.getCode(), dialog.getDesc());

			ModularBean modular = new ModularBean();
			modular.setProject(project);
			modular.setCode(dialog.getCode());
			modular.setDesc(dialog.getDesc());
			project.addModular(modular);
			TreeItem projectItem = bindings.get(project);
			renderModular(projectItem, modular);
		}

	}

	protected void configDb() {
		DbConfigDialog dialog = new DbConfigDialog(shell, SWT.DIALOG_TRIM);
		if (project != null && project.getDbConfig() != null) {
			log.debug("修改数据库配置");
			dialog.setDbConfig(project.getDbConfig());
		} else {
			log.debug("使用默认的MySql数据库配置");
			dialog.setDbConfig(DbConfigBean.defaultMysql());
		}

		DbConfigBean bean = (DbConfigBean) dialog.open();
		if (bean != null) {
			saveFile = false;
			project.setDbConfig(bean);
		}
	}

	private void mockProject() {
		project = new ProjectBean();
		project.setCode("code-generate");
		ModularBean m1 = new ModularBean();
		m1.setCode("AREA");
		ModularBean m2 = new ModularBean();
		m2.setCode("DEVICE");
		project.addModular(m1);
		project.addModular(m2);
	}

	private void renderProject() {
		bindings = new HashMap<DefineBean, TreeItem>();
		tree.removeAll();
		trtmNewTreeitem = new TreeItem(tree, SWT.NONE);
		trtmNewTreeitem.setImage(new Image(Display.getDefault(), "icons/application_home.png"));
		if (project == null) {
			trtmNewTreeitem.setText("新建项目");
		} else {
			project.init();
			trtmNewTreeitem.setText(project.getCode());
			trtmNewTreeitem.setData(project);
			trtmNewTreeitem.setData("bean", project);
			trtmNewTreeitem.setData("type", "project");
			bindings.put(project, trtmNewTreeitem);
			Map<String, ModularBean> modularsMap = project.getModulars();
			if (modularsMap != null) {
				Collection<ModularBean> modulars = modularsMap.values();
				for (ModularBean modularBean : modulars) {
					renderModular(trtmNewTreeitem, modularBean);
				}
			}
		}
	}

	private void renderModular(TreeItem parent, ModularBean modularBean) {
		modularBean.init();
		TreeItem item = new TreeItem(parent, SWT.NONE);
		item.setText(modularBean.getCode());
		item.setData("bean", modularBean);
		item.setData("type", "modular");
		item.setExpanded(true);
		item.setImage(new Image(Display.getDefault(), "icons/folder.png"));
		bindings.put(modularBean, item);

		List<EntityBean> entities = modularBean.getEntities();
		if (entities == null || entities.size() == 0) {
			return;
		}

		for (EntityBean entityBean : entities) {
			renderEntity(item, entityBean);
		}
	}

	private void renderEntity(TreeItem parent, EntityBean entityBean) {
		entityBean.init();
		TreeItem item = new TreeItem(parent, SWT.NONE);
		item.setText(entityBean.getCode());
		item.setData("bean", entityBean);
		item.setData("type", "entity");
		item.setExpanded(true);
		item.setImage(new Image(Display.getDefault(), "icons/page.png"));
		bindings.put(entityBean, item);

		TreeItem cols = new TreeItem(item, SWT.NONE);
		cols.setText("字段");
		cols.setData("bean", entityBean);
		cols.setData("type", "column");
		cols.setExpanded(true);
		cols.setImage(new Image(Display.getDefault(), "icons/application_view_columns.png"));

		TreeItem cls = new TreeItem(item, SWT.NONE);
		cls.setText("类");
		cls.setData("bean", entityBean);
		cls.setData("type", "class");
		cls.setExpanded(true);
		cls.setImage(new Image(Display.getDefault(), "icons/class_obj.gif"));

	}

}
